class Solution(object):
    def reorderLogFiles(self, logs):
        """
        :type logs: List[str]
        :rtype: List[str]
        """
        n = len(logs)
        str_list = []
        num_list = []
        for i in range(n):
            if ord('0') <= ord(logs[i][-1]) <= ord('9'):
                num_list.append(logs[i])
            else:
                temp = logs[i].split(' ')
                str_list.append([' '.join(temp[1:]), temp[0]])
        str_list.sort()
        out = [item[1] + ' ' + item[0] for item in str_list] + num_list
        return out

data = Solution()
logs = ["a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo"]
print(data.reorderLogFiles(logs))